package com.cyys.modules.mobile;

import com.cyys.common.ql.AR;
import com.cyys.common.utils.MD5Util;
import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.jwt.JwtUtils;
import com.cyys.modules.core.sysParam.SystemConfigureUtil;
import com.cyys.modules.core.sysParam.SystemConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/token")
@Api(tags = "token接口")
public class TokenAction extends BaseAction {

	@ApiOperation(value = "token获取")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cipherCode", value = "密钥", required = true, dataType = "String", paramType = "query")
	})
	@GetMapping(value = "/getToken")
	public AR getToken(String cipherCode) throws Exception {
		if(StringUtil.isBlank(cipherCode)){
			if(!MD5Util.calcMD5(cipherCode).equals(SystemConfigureUtil.get(SystemConstants.TOKEN_CIPHER_CODE))){
				return AR.failed("秘钥错误");
			}
		}
		return AR.ok(JwtUtils.createJWT(UUID.randomUUID().toString(), null,
				3600 * 1000 * 24),"获取成功");
	}

}
